package com.example.yqzs.controller;


import com.example.yqzs.pojo.User;
import com.example.yqzs.pojo.UserExample;
import com.example.yqzs.service.UserService;
import com.example.yqzs.util.DateUtil;
import com.example.yqzs.util.ReturnUtil;
import com.example.yqzs.util.UuidUtil;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.swing.plaf.ToolTipUI;
import javax.validation.Valid;
import java.util.List;

@RequestMapping
@RestController
public class UserController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserService userService;

    //以下是注册
    @ResponseBody
    @RequestMapping(value = "/member/userRegister",method = RequestMethod.POST)
    public ModelMap userRegister(@Validated @RequestBody User user, BindingResult result){
        try {
            if(StringUtils.isEmpty(user.getUserLoginName())){
                return ReturnUtil.error("用户名不能为空", null, null);
            }
            if(StringUtils.isEmpty(user.getUserLoginPwd())){
                return ReturnUtil.error("密码不能为空", null, null);
            }

            UserExample example = new UserExample();
            example.createCriteria().andUserLoginNameEqualTo(user.getUserLoginName());
            int userCount = userService.getCount(example);
            if (userCount > 0) {
                return ReturnUtil.error("用户名已存在", null, null);
            }

            String id = UuidUtil.getUUID();
            user.setId(id);
            user.setPlaintPwd(user.getUserLoginPwd());
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String password = passwordEncoder.encode(user.getUserLoginPwd());
            user.setUserLoginPwd(password);
            user.setUserLevel("0");
            user.setCreateTime(DateUtil.getCurrentTime());
            user.setUserLoginName(user.getUserLoginName());
            user.setCreateUserId(UuidUtil.runVerifyCode(10));
            userService.setInsert(user);

            return ReturnUtil.success("注册用户信息成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }

    }

    //以下是登录
    @ResponseBody
    @RequestMapping(value = "/member/userLogin",method = RequestMethod.POST)
    public ModelMap userLogin(@Validated @RequestBody User user, BindingResult result){
        try {
            if(StringUtils.isEmpty(user.getUserLoginName())){
                return ReturnUtil.error("用户名不能为空", null, null);
            }
            if(StringUtils.isEmpty(user.getUserLoginPwd())){
                return ReturnUtil.error("密码不能为空", null, null);
            }

            UserExample example = new UserExample();
            example.createCriteria().andUserLoginNameEqualTo(user.getUserLoginName());
            int userCountName = userService.getCount(example);
            if (userCountName == 0){
                return ReturnUtil.error("用户名不存在", null, null);
            }

            UserExample example1 = new UserExample();
            example1.createCriteria().andPlaintPwdEqualTo(user.getUserLoginPwd());
            int userCountPased = userService.getCount(example1);
            if (userCountPased == 0){
                return ReturnUtil.error("密码错误", null, null);
            }

            User users = userService.getByUser(user.getUserLoginName());
            return ReturnUtil.success("获取用户信息成功", users);

        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

    //以下是修改用户信息
    @ResponseBody
    @RequestMapping(value = "/member/userInformation",method = RequestMethod.POST)
    public ModelMap userInformation(@Validated @RequestBody User user, BindingResult result){
        try {
            if(StringUtils.isEmpty(user.getId())){
                return ReturnUtil.error("用户ID不能为空", null, null);
            }

            if (user.getUserName() != null){
                user.setUserName(user.getUserName());
            }
            if (user.getUserTel() != null){
                user.setUserTel(user.getUserTel());
            }
            if (user.getUserSex() != null){
                user.setUserSex(user.getUserSex());
            }
            if (user.getUserHeadUrl() != null){
                user.setUserHeadUrl(user.getUserHeadUrl());
            }
            if (user.getUserAddress() != null){
                user.setUserAddress(user.getUserAddress());
            }
            if (user.getUserBrithday() != null){
                user.setUserBrithday(user.getUserBrithday());
            }
            if (user.getUserPhone() != null){
                user.setUserPhone(user.getUserPhone());
            }
            if (user.getUserEmail() != null){
                user.setUserEmail(user.getUserEmail());
            }

            UserExample example = new UserExample();
            example.createCriteria().andIdEqualTo(user.getId());
            int count = userService.setUpdateByUser(user,example);
            if (count == 0){
                return ReturnUtil.error("修改用户信息失败");
            }else {
                return ReturnUtil.success("修改用户信息成功");
            }


        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

    //以下是修改用户密码
    @ResponseBody
    @RequestMapping(value = "/member/userPassword",method = RequestMethod.POST)
    public ModelMap userPassword(@Validated @RequestBody User user, BindingResult result){
        try {
            if(StringUtils.isEmpty(user.getId())){
                return ReturnUtil.error("用户ID不能为空", null, null);
            }

            if (user.getUserLoginPwd() != null){
                BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
                String password = passwordEncoder.encode(user.getUserLoginPwd());
                user.setUserLoginPwd(password);
            }else {
                return ReturnUtil.error("用户密码不能为空", null, null);
            }

            UserExample example = new UserExample();
            example.createCriteria().andIdEqualTo(user.getId());
            int count = userService.setUpdateByUser(user,example);
            if (count == 0){
                return ReturnUtil.error("修改用户信息失败");
            }else {
                return ReturnUtil.success("修改用户信息成功");
            }


        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

    //以下是用户ID查信息
    @ResponseBody
    @RequestMapping(value = "/member/getUserInfo",method = RequestMethod.POST)
    public ModelMap getUserInfo(@Validated @RequestBody User user, BindingResult result){
        try {
            if(StringUtils.isEmpty(user.getId())){
                return ReturnUtil.error("用户ID不能为空", null, null);
            }

            User user1 = userService.getUserInformation(user.getId());
            if (user1 == null){
                return ReturnUtil.error("获取用户信息失败");
            }else {
                return ReturnUtil.success("获取用户信息成功", user1);
            }

        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

    //以下是测试
    @ResponseBody
    @RequestMapping(value = "/member/getTest",method = {RequestMethod.GET, RequestMethod.POST})
    public ModelMap getTest(@Validated @RequestBody User user, BindingResult result){
        try {

            if(StringUtils.isEmpty(user.getId())){
                return ReturnUtil.error("用户ID不能为空", null, null);
            }

            int user1 = userService.deleteByPrimaryKey(user.getId());
            if (user1 == 0){
                return ReturnUtil.error("获取用户信息失败");
            }else {
                return ReturnUtil.success("获取用户信息成功", user1);
            }


        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

}
